package com.itheima.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.itheima.common.R;
import com.itheima.common.TokenHolder;
import com.itheima.domain.Dish;
import com.itheima.domain.Employee;
import com.itheima.domain.Taste;
import com.itheima.dto.AddDishDto;
import com.itheima.service.DishService;
import com.itheima.vo.DishPageVo;
import com.itheima.vo.DishVo;
import com.itheima.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * date: 2022/5/18 16:51
 *
 * @author daya000
 */
@RestController
@RequestMapping("dish")
public class DishController {
    @Autowired
    private DishService service;
    @PostMapping("")
    public Result addDish(@RequestBody AddDishDto dto){


        //1.判断参数是否为空
        if (Objects.isNull(dto)){
            return new Result(0,null,"参数不能为空",null);
        }
        //2.判断新增分类是否存在数据库
        Dish nameExist = service.selectByName(dto.getName());
        if (nameExist != null) {
            return new Result(0,null, "菜品名已存在", null);
        }

        //雪花算法
        Snowflake snowflake = IdUtil.createSnowflake(1,1);

        //这个是dish表里面的id
        String id = snowflake.nextIdStr();
        String code = RandomUtil.randomNumbers(5);

        //哪里没有补哪里
        Dish dish = new Dish();

        //Cv大帝复制实体类
        BeanUtil.copyProperties(dto,dish);

        dish.setId(id);
        dish.setType(1);
        dish.setSort(10);
        dish.setCreateTime(new Date());
        dish.setUpdateTime(new Date());
        //从token那么存储拿过来的，别问
        dish.setCreateUser(TokenHolder.getCurrentId());
        dish.setUpdateUser(TokenHolder.getCurrentId());
        service.addDish(dish);

        //遍历循环，别问List哪里来的，他从DishAddDto来的
        List<Taste> flavors = dto.getFlavors();
        //报错异常处理，判断一个菜品是否有添加口味
        if (ObjectUtil.isNotNull(flavors)){

            for (Taste flavor : flavors) {
                Taste taste = new Taste();

                //每次遍历生成一个唯一的id
                String tasteId = snowflake.nextIdStr();

                //哪里没有补哪里，步步高打火机
                taste.setId(tasteId);
                taste.setDishId(id);
                taste.setName(flavor.getName());
                taste.setValue(flavor.getValue());

                taste.setCreateTime(new Date());
                taste.setUpdateTime(new Date());
                taste.setCreateUser(TokenHolder.getCurrentId());
                taste.setUpdateUser(TokenHolder.getCurrentId());
                taste.setIsDeleted(0);

                //终极套娃
                service.addTaste(taste);
            }
        }

        return new Result(1,null,"添加菜品成功",null);
    }


    @GetMapping("/page")
    public Result selectByPage(@RequestParam(value = "name", required = false) String name,
                               @RequestParam(value = "page", defaultValue = "1") Integer page,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                               @RequestParam(value ="type",required = false) String type){
        if (page <= 0) {
            return new Result(0, "页面错误", null, null);
        }

        //获取分页数据
        List<Dish> dishes = service.selectByPage(name,page, pageSize);
        //获取总条数
        Long count = service.selectTotal(name);
        DishPageVo vo = new DishPageVo();
        vo.setRecords(dishes);
        vo.setTotal(count);
        vo.setSize(pageSize);
        vo.setCurrent(page);
        return new Result(1,null,vo,null);

    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable String id){
        if (Objects.isNull(id)){
            return new Result(0,null,"id不能为空",null);
        }
        Dish dish = service.selectById(id);
        List<Taste> tastes = service.selectByDishId(id);
        DishVo dishVo = new DishVo();
        BeanUtils.copyProperties(dish,dishVo);
        dishVo.setFlavors(tastes);
        return new Result(1,null,dishVo,null);

    }

    @PutMapping("")
    public Result updateDish(@RequestBody DishVo vo){
        Dish dish = new Dish();
        BeanUtils.copyProperties(vo,dish);
        service.updateDish(dish);

        List<Taste> flavors = vo.getFlavors();
        service.deleteByDishId(vo.getId());

        if (CollectionUtils.isNotEmpty(flavors)){
            for (Taste flavor : flavors) {
                String id = new Snowflake().nextIdStr();
                flavor.setId(id);
                flavor.setDishId(vo.getId());
                service.addTaste(flavor);
            }

        }
        return new Result(1,null,"修改菜品成功",null);

    }

    @DeleteMapping
    public Result deleteDishById(@RequestParam("ids") List<String> ids){
        for (String id : ids) {
            service.deleteDishById(id);
        }

        return new Result(1,null,"删除菜品成功",null);
    }

    @GetMapping("list")
    public Result dishList(@RequestParam String categoryId){
        if (ObjectUtil.isNull(categoryId)){
            return new Result(0,null,"参数不能为空",null);
        }
        List<Dish> dishes = service.selectByCategoryId(categoryId);
        return new Result(1,null,dishes,null);
    }

    @PostMapping("status/{status}")
    public Result updateStatus(
            @PathVariable("status") Integer status,
            @RequestParam("ids") List<String> ids){
        for (String id : ids) {
            service.updateStatus(status,id);
        }
        return new Result(1,null,"操作成功",null);
    }

}
